/**
 * 
 */
package org.platform.template.aliyun;

import java.io.File;
import java.io.InputStream;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.platform.common.utils.Assert;


/**
 * <pre>
 * 功能：阿里云的OSS工具类，需要在spring文件中初始化
 * 
 * &lt;bean id="aliyunOssService" class="com.platform.template.aliyun.AliyunOSSTemplate"&gt;
 * 		&lt;property name="accessKey" value="${aliyun.accessKey}" /&gt;
 * 		&lt;property name="secretKey" value="${aliyun.secretKey}" /&gt;
 * 		&lt;property name="endpoint" value="${aliyun.endpoint}" /&gt;
 * &lt;/bean&gt;
 * 
 * 在properties文件中配置：
 * aliyun.endpoint
 * aliyun.accessKey
 * aliyun.secretKey
 * 
 * </pre>
 * @author wei.gao
 */
public class AliyunOSSTemplate {
	private String accessKey = null;
	private String secretKey = null;
	private String endpoint = null;
	
	@PostConstruct
	private void init(){
		Assert.notEmpty("endpoint", endpoint);
		Assert.notEmpty("accessKey", accessKey);
		Assert.notEmpty("secretKey", secretKey);
	}
	
	@PreDestroy
	public void destory(){
		client.shutdown();
	}
	
	// 客户端对象
	private OSSClient client = null;
	
	/**
	 * <pre>
	 * 功能：获取OSS客户端
	 * </pre>
	 * @return
	 */
	@PostConstruct
	public OSSClient getClient() {
		if(client == null){
			synchronized (AliyunOSSTemplate.class) {
				if(client == null){
					client = new OSSClient(endpoint, accessKey, secretKey);
				}
			}
		}
		return client;
	}
	
	/**
	 * <pre>
	 * 功能：上传文件
	 * </pre>
	 * @param bucketName
	 * @param key
	 * @param file
	 * @return
	 */
	public PutObjectResult upload(String bucketName, String key, File file){
		PutObjectResult result = getClient().putObject(new PutObjectRequest(bucketName, key, file));
		return result;
	}
	
	/**
	 * <pre>
	 * 功能：上传文件流
	 * </pre>
	 * @param bucketName
	 * @param key
	 * @param input
	 * @return
	 */
	public PutObjectResult upload(String bucketName, String key, InputStream input){
		PutObjectResult result = getClient().putObject(new PutObjectRequest(bucketName, key, input));
		return result;
	}
	
	/**
	 * <pre>
	 * 功能：key是否存在
	 * </pre>
	 * @param bucketName
	 * @param key
	 * @return
	 */
	public boolean exist(String bucketName, String key){
		return getClient().doesObjectExist(bucketName, key);
	}
	
	/**
	 * <pre>
	 * 功能：获取key对象
	 * </pre>
	 * @param bucketName
	 * @param key
	 * @return
	 */
	public OSSObject get(String bucketName, String key){
		return getClient().getObject(bucketName, key);
	}
	
	/**
	 * <pre>
	 * 功能：根据正则查找
	 * </pre>
	 * @param bucketName
	 * @param prefix
	 * @return
	 */
	public ObjectListing list(String bucketName, String prefix){
		return getClient().listObjects(bucketName, prefix);
	}
	
	/**
	 * <pre>
	 * 功能：删除key
	 * </pre>
	 * @param bucketName
	 * @param key
	 */
	public void delete(String bucketName, String key){
		getClient().deleteObject(bucketName, key);
	}

	public String getEndpoint() {
		return endpoint;
	}

	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}

	public String getSecretKey() {
		return secretKey;
	}

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}
}
